﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RBAC.Web.Mapper;
using RBAC.Web.Models;
using RBAC.Web.Service;
using RBAC.Web.Utilities;
using RBAC.Web.ListCenter;

namespace RBAC.Web.Controllers
{
    public class PopupController : Controller
    {
		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region User_Role lookup
        public String User_RoleGetExternalLookup(string required, string AutoSearch)
        {
            RoleModel model = new RoleModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.RoleCode = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<RoleModel> Role = User_GetRole(model);
                if (Role.Count == 1)
                {
                    String RoleStr;
                    foreach (var item in Role)
                    {
                        RoleStr = GetlistCenter.User_RoleMapper(requiredFields, item);
						return RoleStr;
                    }

                }
                else if (Role.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult User_RolePopExternalLookup()
        {
            RoleModel model = new RoleModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.RoleCode = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["RoleCode"] != null)
				{
					model.RoleCode = StringParser.GetValue<String>(Request.QueryString["RoleCode"]);
				}
				
			
            if(model.RoleCode != null
						)
            {
                model.ListOfRole = this.User_GetRole(model);
            }
            else
            {
                model.ListOfRole = this.User_GetRole();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<RoleModel> User_GetRole()
        {
            return User_GetRole(null);
        }

        private List<RoleModel> User_GetRole(RoleModel key)
        {

            var result = new List<RoleModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllRoleRequestDto request = new GetAllRoleRequestDto() ;
            if (key != null)
			{
					request.RoleCode = key.RoleCode;
								request.Sort = "RoleCode";
                    request.SortType = "ASC";
			}
            
            result = showList.User_RoleList(request);
            return result;
        }

        #endregion

		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region Permission_Role lookup
        public String Permission_RoleGetExternalLookup(string required, string AutoSearch)
        {
            RoleModel model = new RoleModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.RoleCode = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<RoleModel> Role = Permission_GetRole(model);
                if (Role.Count == 1)
                {
                    String RoleStr;
                    foreach (var item in Role)
                    {
                        RoleStr = GetlistCenter.Permission_RoleMapper(requiredFields, item);
						return RoleStr;
                    }

                }
                else if (Role.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult Permission_RolePopExternalLookup()
        {
            RoleModel model = new RoleModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.RoleCode = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["RoleCode"] != null)
				{
					model.RoleCode = StringParser.GetValue<String>(Request.QueryString["RoleCode"]);
				}
				
			
            if(model.RoleCode != null
						)
            {
                model.ListOfRole = this.Permission_GetRole(model);
            }
            else
            {
                model.ListOfRole = this.Permission_GetRole();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<RoleModel> Permission_GetRole()
        {
            return Permission_GetRole(null);
        }

        private List<RoleModel> Permission_GetRole(RoleModel key)
        {

            var result = new List<RoleModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllRoleRequestDto request = new GetAllRoleRequestDto() ;
            if (key != null)
			{
					request.RoleCode = key.RoleCode;
								request.Sort = "RoleCode";
                    request.SortType = "ASC";
			}
            
            result = showList.Permission_RoleList(request);
            return result;
        }

        #endregion

		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region Permission_Function lookup
        public String Permission_FunctionGetExternalLookup(string required, string AutoSearch)
        {
            FunctionModel model = new FunctionModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.FuncName = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<FunctionModel> Function = Permission_GetFunction(model);
                if (Function.Count == 1)
                {
                    String FunctionStr;
                    foreach (var item in Function)
                    {
                        FunctionStr = GetlistCenter.Permission_FunctionMapper(requiredFields, item);
						return FunctionStr;
                    }

                }
                else if (Function.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult Permission_FunctionPopExternalLookup()
        {
            FunctionModel model = new FunctionModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.FuncName = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["FuncName"] != null)
				{
					model.FuncName = StringParser.GetValue<String>(Request.QueryString["FuncName"]);
				}
				
			
            if(model.FuncName != null
						)
            {
                model.ListOfFunction = this.Permission_GetFunction(model);
            }
            else
            {
                model.ListOfFunction = this.Permission_GetFunction();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<FunctionModel> Permission_GetFunction()
        {
            return Permission_GetFunction(null);
        }

        private List<FunctionModel> Permission_GetFunction(FunctionModel key)
        {

            var result = new List<FunctionModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllFunctionRequestDto request = new GetAllFunctionRequestDto() ;
            if (key != null)
			{
					request.FuncName = key.FuncName;
								request.Sort = "FuncName";
                    request.SortType = "ASC";
			}
            
            result = showList.Permission_FunctionList(request);
            return result;
        }

        #endregion

		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region Permission_RoleLookup lookup
        public String Permission_RoleLookupGetExternalLookup(string required, string AutoSearch)
        {
            RoleModel model = new RoleModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.RoleCode = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<RoleModel> RoleLookup = Permission_GetRoleLookup(model);
                if (RoleLookup.Count == 1)
                {
                    String RoleLookupStr;
                    foreach (var item in RoleLookup)
                    {
                        RoleLookupStr = GetlistCenter.Permission_RoleLookupMapper(requiredFields, item);
						return RoleLookupStr;
                    }

                }
                else if (RoleLookup.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult Permission_RoleLookupPopExternalLookup()
        {
            RoleModel model = new RoleModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.RoleCode = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["RoleCode"] != null)
				{
					model.RoleCode = StringParser.GetValue<String>(Request.QueryString["RoleCode"]);
				}
				
			
            if(model.RoleCode != null
						)
            {
                model.ListOfRole = this.Permission_GetRoleLookup(model);
            }
            else
            {
                model.ListOfRole = this.Permission_GetRoleLookup();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<RoleModel> Permission_GetRoleLookup()
        {
            return Permission_GetRoleLookup(null);
        }

        private List<RoleModel> Permission_GetRoleLookup(RoleModel key)
        {

            var result = new List<RoleModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllRoleRequestDto request = new GetAllRoleRequestDto() ;
            if (key != null)
			{
					request.RoleCode = key.RoleCode;
								request.Sort = "RoleCode";
                    request.SortType = "ASC";
			}
            
            result = showList.Permission_RoleLookupList(request);
            return result;
        }

        #endregion

		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region Function_Domain lookup
        public String Function_DomainGetExternalLookup(string required, string AutoSearch)
        {
            DomainModel model = new DomainModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.DomainName = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<DomainModel> Domain = Function_GetDomain(model);
                if (Domain.Count == 1)
                {
                    String DomainStr;
                    foreach (var item in Domain)
                    {
                        DomainStr = GetlistCenter.Function_DomainMapper(requiredFields, item);
						return DomainStr;
                    }

                }
                else if (Domain.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult Function_DomainPopExternalLookup()
        {
            DomainModel model = new DomainModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.DomainName = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["DomainName"] != null)
				{
					model.DomainName = StringParser.GetValue<String>(Request.QueryString["DomainName"]);
				}
				
			
            if(model.DomainName != null
						)
            {
                model.ListOfDomain = this.Function_GetDomain(model);
            }
            else
            {
                model.ListOfDomain = this.Function_GetDomain();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<DomainModel> Function_GetDomain()
        {
            return Function_GetDomain(null);
        }

        private List<DomainModel> Function_GetDomain(DomainModel key)
        {

            var result = new List<DomainModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllDomainRequestDto request = new GetAllDomainRequestDto() ;
            if (key != null)
			{
					request.DomainName = key.DomainName;
								request.Sort = "DomainName";
                    request.SortType = "ASC";
			}
            
            result = showList.Function_DomainList(request);
            return result;
        }

        #endregion

		
        /* Summary
         * one lookup attribute has 4 method in controller and can change code / name to another attr.
         * 
         * 1. XXXGetExternalLookup 
         * 2. XXXPopExternalLookup()
         * 3. GetXXX() -> get All 
         * 4. GetXXX(model key) -> get filter list
         */

		#region Function_DomainLookup lookup
        public String Function_DomainLookupGetExternalLookup(string required, string AutoSearch)
        {
            DomainModel model = new DomainModel();
            if (!String.IsNullOrEmpty(AutoSearch) && !String.IsNullOrEmpty(required))
            {
				model.DomainName = StringParser.GetValue<String>(AutoSearch);
				string[] requiredFields = required.Split('|');
                List<DomainModel> DomainLookup = Function_GetDomainLookup(model);
                if (DomainLookup.Count == 1)
                {
                    String DomainLookupStr;
                    foreach (var item in DomainLookup)
                    {
                        DomainLookupStr = GetlistCenter.Function_DomainLookupMapper(requiredFields, item);
						return DomainLookupStr;
                    }

                }
                else if (DomainLookup.Count > 1)
                {
                    return "MoreOne";
                }
            }
            return "NotFound";
        }

        public ActionResult Function_DomainLookupPopExternalLookup()
        {
            DomainModel model = new DomainModel();
            if (Request.QueryString["AutoSearch"] != null)
            {
				model.DomainName = StringParser.GetValue<String>(Request.QueryString["AutoSearch"]);
            }

							if (Request.QueryString["DomainName"] != null)
				{
					model.DomainName = StringParser.GetValue<String>(Request.QueryString["DomainName"]);
				}
				
			
            if(model.DomainName != null
						)
            {
                model.ListOfDomain = this.Function_GetDomainLookup(model);
            }
            else
            {
                model.ListOfDomain = this.Function_GetDomainLookup();
            }

            if (!String.IsNullOrEmpty(Request.QueryString["required"]))
            {
                model.RequiredFields = Request.QueryString["required"].Split('|');
            }
            
            return View(model);
        }

		private List<DomainModel> Function_GetDomainLookup()
        {
            return Function_GetDomainLookup(null);
        }

        private List<DomainModel> Function_GetDomainLookup(DomainModel key)
        {

            var result = new List<DomainModel>();
            var service = new ServiceClient();
			var showList = new GetlistCenter();

            GetAllDomainRequestDto request = new GetAllDomainRequestDto() ;
            if (key != null)
			{
					request.DomainName = key.DomainName;
								request.Sort = "DomainName";
                    request.SortType = "ASC";
			}
            
            result = showList.Function_DomainLookupList(request);
            return result;
        }

        #endregion

		    }
}